// 官方文档自定义server
const Koa = require('koa')
const { parse } = require('url')
const next = require('next')

const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()
const proxy = require('koa2-nginx');

app.prepare().then(() => {
  const koaApp = new Koa();
  koaApp.use(
    proxy({ '/api': { target: 'https://blog.yasol.cn', changeOrigin: true } })
  );
  koaApp.use(async (ctx,next) => {
    const {req,res} = ctx
    const parsedUrl = parse(req.url, true)
    const { pathname, query } = parsedUrl
    /*
    if (pathname === '/b') {
      app.render(req, res, '/a', query)
    } else {
      await handle(req, res, parsedUrl)
    }
    */
    await handle(req, res, parsedUrl)
  }).listen(3005, err => {
    if (err) throw err
    console.log('> Ready on http://localhost:3005')
  });
})
